﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Modules;
    using DotNetNuke.Services.Exceptions;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class MiniCart : BaseModule
    {
        private CartList _CartList;
        [AccessedThroughProperty("cartlist1")]
        private CartList _cartlist1;
        [AccessedThroughProperty("PlaceHolder1")]
        private PlaceHolder _PlaceHolder1;
        [AccessedThroughProperty("PlaceHolder2")]
        private PlaceHolder _PlaceHolder2;
        private int ORID;

        public MiniCart()
        {
            base.Load += new EventHandler(this.Page_Load);
            this.ORID = -1;
        }

        private void cartlist1_RecalculateCart()
        {
            this.Response.Redirect(Globals.NavigateURL(this.TabId, "", SharedFunctions.getUrlCookieInfo(this.PortalId, new string[] { "" })));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                SettingsController controller = new SettingsController();
                string strSourceText = "";
                string settingName = "";
                CartTotals calulatedTotals = CurrentCart.GetCalulatedTotals(this.PortalId, SharedFunctions.GetDefaultShipMethod(this.PortalId));
                int num = 0;
                if (Versioned.IsNumeric(Conversions.ToString(this.Settings["ddlMiniPosition"])))
                {
                    num = Conversions.ToInteger(this.Settings["ddlMiniPosition"]);
                }
                if (calulatedTotals != null)
                {
                    if (calulatedTotals.Qty == 0)
                    {
                        if (Conversions.ToString(this.Settings["ddlEmptyTemplate"]) == "")
                        {
                            settingName = "minicartempty.template";
                        }
                        else
                        {
                            settingName = Conversions.ToString(this.Settings["ddlEmptyTemplate"]);
                        }
                        this.cartlist1.Visible = false;
                    }
                    else
                    {
                        if (Conversions.ToString(this.Settings["ddlFullTemplate"]) == "")
                        {
                            settingName = "minicart.template";
                        }
                        else
                        {
                            settingName = Conversions.ToString(this.Settings["ddlFullTemplate"]);
                        }
                        if (Conversions.ToBoolean(this.Settings["chkShowFullCart"]))
                        {
                            this.SetUpCartList();
                        }
                        else
                        {
                            this.cartlist1.Visible = false;
                        }
                    }
                    NB_Store_SettingsTextInfo info = controller.GetSettingsText(this.PortalId, settingName, SharedFunctions.GetCurrentCulture());
                    if ((info != null) && (info.SettingText != ""))
                    {
                        strSourceText = info.SettingText;
                        strSourceText = new TokenStoreReplace(calulatedTotals, SharedFunctions.GetCurrentCulture(), this.PortalId).DoTokenReplace(strSourceText);
                    }
                }
                switch (num)
                {
                    case 0:
                        this.PlaceHolder1.Controls.Add(new LiteralControl(this.Server.HtmlDecode(strSourceText)));
                        break;

                    case 1:
                        this.PlaceHolder2.Controls.Add(new LiteralControl(this.Server.HtmlDecode(strSourceText)));
                        break;
                }
            }
            catch (Exception exception1)
            {
                ProjectData.SetProjectError(exception1);
                Exception exc = exception1;
                DotNetNuke.Services.Exceptions.Exceptions.ProcessModuleLoadException((PortalModuleBase) this, exc);
                ProjectData.ClearProjectError();
            }
        }

        private void SetUpCartList()
        {
            TaxCalcController controller = new TaxCalcController(this.PortalId);
            int shipMethodID = CurrentCart.GetCurrentCart(this.PortalId).ShipMethodID;
            if (shipMethodID <= 0)
            {
                shipMethodID = SharedFunctions.GetDefaultShipMethod(this.PortalId);
            }
            this.cartlist1.CartID = CurrentCart.GetCurrentCart(this.PortalId).CartID;
            this.cartlist1.PortalID = Conversions.ToString(this.PortalId);
            this.cartlist1.ShipMethodID = Conversions.ToString(shipMethodID);
            this.cartlist1.TaxOption = controller.getTaxOption();
            this.cartlist1.ResourceFile = this.LocalResourceFile;
            this.cartlist1.ShowDiscountCol = Conversions.ToBoolean(this.Settings["chkShowDiscountCol"]);
            this.cartlist1.ShippingHidden = true;
            this.cartlist1.NoUpdates = !Conversions.ToBoolean(this.Settings["chkAllowEdit"]);
            this.cartlist1.HideTotals = Conversions.ToBoolean(this.Settings["chkHideTotal"]);
            this.cartlist1.HideTotals = Conversions.ToBoolean(this.Settings["chkHideTotal"]);
            if (Conversions.ToBoolean(this.Settings["chkHideHeader"]))
            {
                this.cartlist1.HideHeader = true;
            }
            if (Conversions.ToBoolean(this.Settings["chkHideDescriptionCol"]))
            {
                this.cartlist1.HideColumn = 1;
            }
            if (Conversions.ToBoolean(this.Settings["chkHidePriceCol"]))
            {
                this.cartlist1.HideColumn = 2;
            }
            if (Conversions.ToBoolean(this.Settings["chkHideQtyCol"]))
            {
                this.cartlist1.HideColumn = 3;
            }
            if (!Conversions.ToBoolean(this.Settings["chkShowDiscountCol"]))
            {
                this.cartlist1.HideColumn = 4;
            }
            if (Conversions.ToBoolean(this.Settings["chkHideRemoveCol"]))
            {
                this.cartlist1.HideColumn = 5;
            }
            if (Conversions.ToBoolean(this.Settings["chkHideTotalCol"]))
            {
                this.cartlist1.HideColumn = 6;
            }
        }

        protected virtual CartList cartlist1
        {
            get
            {
                return this._cartlist1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                CartList.RecalculateCartEventHandler handler = new CartList.RecalculateCartEventHandler(this.cartlist1_RecalculateCart);
                if (this._cartlist1 != null)
                {
                    this._cartlist1.RecalculateCart -= handler;
                }
                this._cartlist1 = value;
                if (this._cartlist1 != null)
                {
                    this._cartlist1.RecalculateCart += handler;
                }
            }
        }

        protected virtual PlaceHolder PlaceHolder1
        {
            get
            {
                return this._PlaceHolder1;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._PlaceHolder1 = value;
            }
        }

        protected virtual PlaceHolder PlaceHolder2
        {
            get
            {
                return this._PlaceHolder2;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                this._PlaceHolder2 = value;
            }
        }
    }
}

